<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Upload_handle extends MY_Controller
{
    function file()
    {
        $file = current( $_FILES );
        if (! isset( $this->upload_folder['doc'] ))
        {
            $this->rs->err( '系统配置错误！' );
        }
        if (empty( $file ))
        {
            $this->rs->err( '没有文件！' );
        }
        $types = array (
                'zip',
                'rar',
                'txt',
                'doc',
                'xls',
                'pps',
                'docx',
                'xlsx',
                'ppsx' 
        );
        $ex_name = strtolower( pathinfo( $file['name'], PATHINFO_EXTENSION ) );
        if (! in_array( $ex_name, $types ))
        {
            $this->rs->err( '该类型文件不允许上传！' );
        }
        $save_name = $this->create_name() . '.' . $ex_name;
        $save_path = FCPATH . 'public/' . $this->upload_folder['doc'];
        $move_result = $this->move_file( $file, $save_path, $save_name, true );
        if ($move_result['success'])
        {
            $result = array ();
            $result['name'] = $file['name'];
            $result['type'] = $ex_name;
            $result['size'] = $file['size'];
            $result['save'] = $move_result['save_directory'] . $save_name;
            $this->rs->ok( $result );
        }
        else
        {
            $this->rs->err( $move_result['msg'] );
        }
    }
    function image()
    {
        $file = current( $_FILES );
        if (! isset( $this->upload_folder['img'] ))
        {
            $this->rs->err( '系统配置错误！' );
        }
        if (empty( $file ))
        {
            $this->rs->err( '没有文件！' );
        }
        if ($file['size'] > 512000)
        {
            $this->rs->err( '图片文件过大，请处理后再上传！' );
        }
        $types = array (
                'jpg',
                'png',
                'gif' 
        );
        $ex_name = strtolower( pathinfo( $file['name'], PATHINFO_EXTENSION ) );
        if (! in_array( $ex_name, $types ))
        {
            $this->rs->err( '该类型文件不允许上传！' );
        }
        $save_name = $this->create_name() . '.' . $ex_name;
        $save_path = FCPATH . 'public/' . $this->upload_folder['img'];
        $move_result = $this->move_file( $file, $save_path, $save_name, true );
        if ($move_result['success'])
        {
            $result = array ();
            $result['name'] = $file['name'];
            $result['type'] = $ex_name;
            $result['size'] = $file['size'];
            $result['save'] = $move_result['save_directory'] . $save_name;
            $this->rs->ok( $result );
        }
        else
        {
            $this->rs->err( $move_result['msg'] );
        }
    }
    function headimg()
    {
        $file = current( $_FILES );
        if (! isset( $this->upload_folder['headimg'] ))
        {
            $this->rs->err( '系统配置错误！' );
        }
        if (empty( $file ))
        {
            $this->rs->err( '没有文件！' );
        }
        if ($file['size'] > 512000)
        {
            $this->rs->err( '图片文件过大，请处理后再上传！' );
        }
        $types = array (
                'jpg',
                'png',
                'gif' 
        );
        $ex_name = strtolower( pathinfo( $file['name'], PATHINFO_EXTENSION ) );
        if (! in_array( $ex_name, $types ))
        {
            $this->rs->err( '该类型文件不允许上传！' );
        }
        $save_name = $this->create_name() . '.' . $ex_name;
        $save_path = FCPATH . 'public/' . $this->upload_folder['headimg'];
        $move_result = $this->move_file( $file, $save_path, $save_name );
        if ($move_result['success'])
        {
            $result = array ();
            $result['name'] = $file['name'];
            $result['type'] = $ex_name;
            $result['size'] = $file['size'];
            $result['save'] = $move_result['save_directory'] . $save_name;
            $this->rs->ok( $result );
        }
        else
        {
            $this->rs->err( $move_result['msg'] );
        }
    }
    private function move_file($file, $path, $save_name, $is_sub_directory = false)
    {
        $result = array ();
        $result['success'] = false;
        
        if ($file['error'] != 0)
        {
            $error_msg = array (
                    0 => 'There is no error, the file uploaded with success',
                    1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
                    2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
                    3 => 'The uploaded file was only partially uploaded',
                    4 => 'No file was uploaded',
                    6 => 'Missing a temporary folder',
                    7 => 'Failed to write file to disk' 
            );
            $result['msg'] = $error_msg[$file['error']];
            return $result;
        }
        
        if (! is_uploaded_file( $file['tmp_name'] ))
        {
            $result['msg'] = 'Is not a uploaded file';
            return $result;
        }
        
        $sub_directory = '';
        if ($is_sub_directory)
        {
            if (! is_dir( $path ) or ! is_really_writable( $path ))
            {
                $result['msg'] = 'Unable to create directory';
                return $result;
            }
            
            $sub_directory = date( 'Ymd' ) . '/';
            $path .= $sub_directory;
            
            if (! file_exists( $path ))
            {
                if (! @mkdir( $path, 0666, true ))
                {
                    $result['msg'] = 'Create directory error';
                    return $result;
                }
            }
        }
        
        if (! is_dir( $path ) or ! is_really_writable( $path ))
        {
            $result['msg'] = 'Unable to write file';
            return $result;
        }
        
        if (@move_uploaded_file( $file['tmp_name'], $path . $save_name ))
        {
            $result['success'] = true;
            $result['save_directory'] = $sub_directory;
            return $result;
        }
        $result['msg'] = 'Move file error';
        return $result;
    }
    private function create_name()
    {
        $name = chr( 65 + date( 'Y' ) % 27 );
        $name .= dechex( date( 'm' ) );
        $name .= date( 'd' );
        $name .= substr( time(), - 5 );
        // $name .= substr( microtime(), 2, 5 );
        $name .= rand( 1000, 9999 );
        return $name;
    }
}